package com.brian.desktools.features

import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import desktools.composeapp.generated.resources.Res
import desktools.composeapp.generated.resources.binary_conversion_10
import desktools.composeapp.generated.resources.binary_conversion_16
import desktools.composeapp.generated.resources.binary_conversion_2
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
@Preview
fun BinaryConversionScreen() {
    var input2Text by remember { mutableStateOf("") }
    var input10Text by remember { mutableStateOf("") }
    var input16Text by remember { mutableStateOf("") }
    Box(Modifier.fillMaxWidth().fillMaxHeight(), contentAlignment = Alignment.Center) {
        Column(horizontalAlignment = Alignment.Start) {
            Text(stringResource(Res.string.binary_conversion_10))
            TextField(value = input10Text, onValueChange = {
                val input = it.trim().ifBlank { "0" }
                input10Text = input
                input16Text = input10Text.toInt(10).toString(16)
                input2Text = input10Text.toInt(10).toString(2)
            })

            Spacer(Modifier.height(24.dp))

            Text(stringResource(Res.string.binary_conversion_2))
            TextField(value = input2Text, onValueChange = {
                val input = it.trim().ifBlank { "0" }
                input2Text = input
                input10Text = input2Text.toInt(2).toString(10)
                input16Text = input2Text.toInt(2).toString(16)
            })

            Spacer(Modifier.height(24.dp))

            Text(stringResource(Res.string.binary_conversion_16))
            TextField(value = input16Text, onValueChange = {
                val input = it.trim().ifBlank { "0" }
                input16Text = input
                input10Text = input16Text.toInt(16).toString(10)
                input2Text = input10Text.toInt(16).toString(2)
            })
        }
    }
}